﻿$(function () {
    $("#btnAddWebsiteType").click(function () { addWebsiteType(); });
    $("#btnAddWebsite").click(function () { addWebsite(); });
    $("#btnAddWebsiteJK").click(function () { addWebsiteJK(); });
    $("#btnSaveWebsiteType").click(function () { saveWebsiteType(); });
    $("#btnSaveWebsite").click(function () { saveWebsite(); });
    $("#btnSaveWebsiteJK").click(function () { saveWebsiteJK(); });
    $("#btnGetYDM").click(function () { getYDM(); });
    $("#btnGetInfo").click(function () { getInfo(); });
    $("#btnAddHangFire").click(function () { addHangFire(); });
    loadWebsiteTypeTables();    
});

//加载列表数据：网站类型
function loadWebsiteTypeTables() {
    $("#tableBody").html("");   
    $.ajax({
        type: "GET",
        url: "/Webmonitor/WebsiteType/GetAllListToJson?_t=" + new Date().getTime(),
        dataType: "json", 
        success: function (data) {            
            $.each(data, function (i, item) {
                var tr = "<tr>";                
                tr += "<td>" + item.id + "</td>";
                tr += "<td>" + item.websiteTypeName + "</td>";                
                tr += "<td>" + item.sortIndex + "</td>";
                tr += "<td>" + item.remarks + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='editWebsiteType(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteSingleWebsiteType(\"" + item.id + "\")'><i class='fa fa-trash-o'></i>  </button> <button class='btn btn-info btn-xs' href='javascript:;' onclick='loadWebsiteTables(\"" + item.id + "\")'><i class='fa fa-add'></i> 下级网站 </button> </td>"
                tr += "</tr>";
                $("#tableBody").append(tr);
            }) 
        }
    })
}

//新增
function addWebsiteType() {
    $("#Id").val(0);
    $("#Remarks").val("");
    $("#SortIndex").val(0);

    $("#WebsiteTypeName").val("");   

    $("#Title").text("新增类型");
    //弹出新增窗体
    $("#editModalWebsiteType").modal("show");
};
//编辑
function editWebsiteType(id) {
    $.ajax({
        type: "Get",
        url: "/Webmonitor/WebsiteType/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#Id").val(data.id);
            $("#SortIndex").val(data.sortIndex);
            $("#Remarks").val(data.remarks);
            $("#CreatedBy").val(data.createdBy);
            $("#CreatedOn").val(data.createdOn);

            $("#WebsiteTypeName").val(data.websiteTypeName);           
           
            $("#Title").text("编辑类型")
            $("#editModalWebsiteType").modal("show");
        }
    })
};
//保存
function saveWebsiteType() {
    
    var postData = {        
        "Id": $("#Id").val(),
        "Remarks": $("#Remarks").val(),
        "SortIndex": $("#SortIndex").val(),
        "CreatedBy": $("#CreatedBy").val(),
        "CreatedOn": $("#CreatedOn").val(),

        "WebsiteTypeName": $("#WebsiteTypeName").val(),        
    };
    $.ajax({
        type: "Post",
        url: "/Webmonitor/WebsiteType/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadWebsiteTypeTables();
                loadWebsiteTables($("#Id").val());
                $("#editModalWebsiteType").modal("hide");
            } else {
                layer.tips(data.message, "#btnSaveWebsiteType");
            };
        }
    });
};
//删除
function deleteSingleWebsiteType(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    },
        function () {
        $.ajax({
            type: "POST",
            url: "/Webmonitor/WebsiteType/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    loadWebsiteTypeTables()
                    layer.closeAll();
                }
                else {
                    layer.alert(data.message);
                }
            }
        })
    });
};

//加载列表数据：网站（根据网站类型id)
function loadWebsiteTables(websiteTypeId) {
    $("#tableBody1").html(""); 
    $("#WebsiteTypeId").val(websiteTypeId);
    $.ajax({
        type: "GET",
        url: "/Webmonitor/Website/GetAllListByWebsiteTypeToJson?websiteTypeId=" + websiteTypeId + "&_t=" + new Date().getTime(),
        success: function (data) {            
            $.each(data, function (i, item) {
                var tr = "<tr>";
                tr += "<td>" + item.id + "</td>";
                tr += "<td><a href='" + item.websiteUrl + "' target='_blank'>" + item.websiteName + "</a></td>";
                tr += "<td>" + item.sortIndex + "</td>";
                tr += "<td>" + item.remarks + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='editWebsite(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteSingleWebsite(\"" + item.id + "\")'><i class='fa fa-trash-o'></i>  </button>  <button class='btn btn-info btn-xs' href='javascript:;' onclick='loadWebsiteJKTables(\"" + item.id + "\")'><i class='fa fa-add'></i> 下级栏目 </button> </td></td>"
                tr += "</tr>";
                $("#tableBody1").append(tr);
            });
            loadWebsiteJKTables(0);
        }
    })
}

//新增
function addWebsite() {
    if ($("#WebsiteTypeId").val() == "") {
        alert("网站类型为空");
        return;
    }        
    $("#WebsiteTypeId1").val($("#WebsiteTypeId").val());
    $("#Id1").val(0);
    $("#Remarks1").val("");
    $("#SortIndex1").val(0);

    $("#WebsiteName").val("");
    $("#WebsiteUrl").val("");

    $("#Title1").text($("#WebsiteTypeId").val()+"-新增网站");
    //弹出新增窗体
    $("#editModalWebsite").modal("show");
};

//编辑
function editWebsite(id) {
    $.ajax({
        type: "Get",
        url: "/Webmonitor/Website/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#Id1").val(data.id);
            $("#SortIndex1").val(data.sortIndex);
            $("#Remarks1").val(data.remarks);
            $("#CreatedBy1").val(data.createdBy);
            $("#CreatedOn1").val(data.createdOn);

            $("#WebsiteTypeId1").val(data.websiteTypeId);
            $("#WebsiteName").val(data.websiteName);
            $("#WebsiteUrl").val(data.websiteUrl);

            $("#Title1").text("编辑网站")
            $("#editModalWebsite").modal("show");
        }
    })
};

//保存
function saveWebsite() {

    var postData = {
        "Id": $("#Id1").val(),
        "Remarks": $("#Remarks1").val(),
        "SortIndex": $("#SortIndex1").val(),
        "CreatedBy": $("#CreatedBy1").val(),
        "CreatedOn": $("#CreatedOn1").val(),

        "WebsiteTypeId": $("#WebsiteTypeId1").val(),
        "WebsiteName": $("#WebsiteName").val(),
        "WebsiteUrl": $("#WebsiteUrl").val(),
    };
    $.ajax({
        type: "Post",
        url: "/Webmonitor/Website/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadWebsiteTables($("#WebsiteTypeId").val())
                loadWebsiteJKTables($("#Id1").val());
                $("#editModalWebsite").modal("hide");
            } else {
                layer.tips(data.message, "#btnSaveWebsite");
            };
        }
    });
};

//删除
function deleteSingleWebsite(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        $.ajax({
            type: "POST",
            url: "/Webmonitor/Website/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    loadWebsiteTables($("#WebsiteTypeId1").val())
                    layer.closeAll();
                }
                else {
                    layer.alert(data.message);
                }
            }
        })
    });
};

//加载列表数据：栏目（根据网站id)
function loadWebsiteJKTables(websiteId) {
    $("#tableBody2").html("");    
    $("#WebsiteId").val(websiteId);
    $.ajax({
        type: "GET",
        url: "/Webmonitor/WebsiteJK/GetAllListByWebsiteIdToJson?websiteId=" + websiteId + "&_t=" + new Date().getTime(),
        success: function (data) {
            $.each(data, function (i, item) {
                var tr = "<tr>";
                tr += "<td>" + item.id + "</td>";
                tr += "<td><a href='" + item.bigclassUrl + "' target='_blank'>" + item.bigclassName + "</a></td>";                
                tr += "<td>" + item.jkTime + "</td>";                
                tr += "<td>" + (item.isSaveOriginalInfo == true ? "是" : "否") + "</td>";
                tr += "<td>" + (item.isJK == true ? "是" : "否") + "</td>";
                tr += "<td>" + item.sortIndex + "</td>";
                tr += "<td>" + item.remarks + "</td>";
                tr += "<td><button class='btn btn-info btn-xs' href='javascript:;' onclick='editWebsiteJK(\"" + item.id + "\")'><i class='fa fa-edit'></i>  </button> <button class='btn btn-danger btn-xs' href='javascript:;' onclick='deleteSingleWebsiteJK(\"" + item.id + "\")'><i class='fa fa-trash-o'></i>  </button> <button class='btn btn-info btn-xs' href='javascript:;' onclick='testWebsiteJK(\"" + item.id + "\")'><i class=''></i>测试</button></td>"
                tr += "</tr>";
                $("#tableBody2").append(tr);
            })
        }
    })
}

//新增
function addWebsiteJK() {
    if ($("#WebsiteTypeId").val() == "") {
        alert("网站类型为空");
        return;
    } 
    else if ($("#WebsiteId").val() == 0) {
        alert("网站为空");
        return;
    } 
    $("#WebsiteTypeId2").val($("#WebsiteTypeId").val());
    $("#WebsiteId2").val($("#WebsiteId").val());
    $("#Id2").val(0);
    $("#Remarks2").val("");
    $("#SortIndex2").val(0);

    $("#BigclassName").val("");
    $("#BigclassUrl").val("");
    $("#JkTime").val("");
    $("#Coding").val("");
    $("#RegexBlock").val("");
    $("#RegexList").val("");
    $("#PrefixUrl").val("");
    $("#Decryption").val("");
    $("#IsSaveOriginalInfo").prop("checked", false);
    $("#IsJK").prop("checked", true);    

    $("#Title2").text($("#WebsiteId").val()+"-新增栏目");
    //弹出新增窗体
    $("#editModalWebsiteJK").modal("show");
};

//编辑
function editWebsiteJK(id) {
    $.ajax({
        type: "Get",
        url: "/Webmonitor/WebsiteJK/GetByIdToJson?id=" + id + "&_t=" + new Date().getTime(),
        success: function (data) {
            $("#Id2").val(data.id);
            $("#SortIndex2").val(data.sortIndex);
            $("#Remarks2").val(data.remarks);
            $("#CreatedBy2").val(data.createdBy);
            $("#CreatedOn2").val(data.createdOn);

            $("#WebsiteTypeId2").val(data.websiteTypeId);
            $("#WebsiteId2").val(data.websiteId);
            $("#BigclassName").val(data.bigclassName);
            $("#BigclassUrl").val(data.bigclassUrl);
            $("#JkTime").val(data.jkTime);
            $("#Coding").val(data.coding);
            $("#RegexBlock").val(data.regexBlock);
            $("#RegexList").val(data.regexList);
            $("#PrefixUrl").val(data.prefixUrl);
            $("#Decryption").val(data.decryption);
            $("#IsSaveOriginalInfo").prop("checked", data.isSaveOriginalInfo);
            $("#IsJK").prop("checked", data.isJK);

            $("#Title2").text("编辑栏目")
            $("#editModalWebsiteJK").modal("show");
        }
    })
};

//保存
function saveWebsiteJK() {

    var postData = {
        "Id": $("#Id2").val(),
        "Remarks": $("#Remarks2").val(),
        "SortIndex": $("#SortIndex2").val(),
        "CreatedBy": $("#CreatedBy2").val(),
        "CreatedOn": $("#CreatedOn2").val(),

        "WebsiteTypeId": $("#WebsiteTypeId2").val(),
        "WebsiteId": $("#WebsiteId2").val(),
        "BigclassName": $("#BigclassName").val(),
        "BigclassUrl": $("#BigclassUrl").val(),
        "JkTime": $("#JkTime").val(),
        "Coding": $("#Coding").val(),
        "RegexBlock": $("#RegexBlock").val(),
        "RegexList": $("#RegexList").val(),
        "PrefixUrl": $("#PrefixUrl").val(),
        "Decryption": $("#Decryption").val(),
        "IsSaveOriginalInfo": $("#IsSaveOriginalInfo").prop('checked'),
        "IsJK": $("#IsJK").prop('checked')
    };
    $.ajax({
        type: "Post",
        url: "/Webmonitor/WebsiteJK/AddOrModify",
        data: postData,
        success: function (data) {
            if (data.result == "Success") {
                loadWebsiteJKTables($("#WebsiteId2").val())
                $("#editModalWebsiteJK").modal("hide");
            } else {
                layer.tips(data.message, "#btnSaveWebsiteJK");
            };
        }
    });
};

//删除
function deleteSingleWebsiteJK(id) {
    layer.confirm("您确认删除选定的记录吗？", {
        btn: ["确定", "取消"]
    }, function () {
        $.ajax({
            type: "POST",
            url: "/Webmonitor/WebsiteJK/DeleteById",
            data: { "id": id },
            success: function (data) {
                if (data.result == "Success") {
                    loadWebsiteJKTables($("#WebsiteId2").val())
                    layer.closeAll();
                }
                else {
                    layer.alert("删除失败！");
                }
            }
        })
    });
};

//测试
function testWebsiteJK(websiteJKid) {
    $("#WebsiteJKid").val(websiteJKid);
    $("#Title3").text("测试栏目监控");
    $("#testModalWebsiteJK").modal("show");   
};

//获取区块
function getYDM() {
    window.open("/Webmonitor/InfoOriginal/GetBlockStr?websiteJKid=" + $("#WebsiteJKid").val());    
}

//获取信息
function getInfo() {
    window.open("/Webmonitor/InfoOriginal/GetListOriginal?websiteJKid=" + $("#WebsiteJKid").val());
}

//添加到后台任务
function addHangFire() {
    $.ajax({
        type: "Get",
        url: "/Webmonitor/Hangfire/AddHangfire?websiteJKid=" + $("#WebsiteJKid").val(),
        success: function () {
            alert("添加成功")
        }
    })
}